#include "stdint.h"
#include "DAVE.h"
#include "SSC/Src/XMC_ESC.h"//头文件的引用需要注明相对路径

#ifndef _ESC_UART_H_
#define _ESC_UART_H_

/*modbus 03指令期望收到的字节数 x为寄存器个数*/
#define MODBUS_READ_LEN(x) (5+2*x)
#define MODBUS_WRITE_LEN(x) (9+x*2)
#define RX_SIZE_8	8
#define FB_REGS_NUM 14

#define OBJ_OFFSET 2			//对象结构的前2字节是index
#define MOD_READ_DATA_OFFSET 3	//modbus读指令返回帧 数据从第3字节开始

/* modbus read cmd(03) 字段下标 */
#define SLAVE_ADDR	0
#define ADDR_HI		2
#define ADDR_LO		3
#define NUM_REG_HI	4
#define NUM_REG_LO	5
#define BYTE_COUNT 	6

#define ONLINE 1U
#define OFFLINE 0U
#define GRIP_CONNECTED 1U
#define GRIP_DISCONNECTED 0U
#define ENABLE_WRITE 1U

typedef struct GRIPPER_STRUCT
{
	UART_t *uart;//夹爪使用的串口
	bool grip_state;//夹爪连接状态,用来控制状态灯
	uint8_t grip_id;//夹爪ID号，为0时表示没有连接
	uint8_t series_num;//夹爪系列号，用于区分旋转和非旋转夹爪
	bool update_flag;//夹爪cotrol_object成员是否更新
	uint8_t updated_member[12];//用于记录已更新的cotrol_object成员id
	uint8_t rx_buffer[33];//夹爪返回的数据缓存
	TOBJ6000 *cotrol_object;
	TOBJ6020 *feedback_object;
}GRIPPER_OBJECT;

typedef struct UART_PARAM
{
	UART_t *uart;//串口端口
	bool uart_state;//串口端口状态
	uint8_t series_NO;//串口端口接的夹爪系列号
	bool update_enable;
	uint8_t update_member[12];
}UART_PARAM_t;

enum GRIPPER_SERIES_NO
{
	PGE_SERIES=2, RGI_SERIES=5
};

enum GRIPPER_CMD_NO
{
	No_Update, Init_cmd, Force_cmd, Position_cmd, Speed_cmd, AngleLo_cmd, AngleHi_cmd, \
	RotaSpeed_cmd, RotaForce_cmd, AngleRelat_cmd, StopRota_cmd, ResetRotaVal_cmd, ControlWord_cmd
};

enum GRIPPER_REG_ADDR
{
	Init_addr=0x0100, Force_addr=0x0101, Position_addr=0x0103, Speed_addr=0x0104,\
	AngleLo_addr=0x0105, AngleHi_addr=0x0106,RotaSpeed_addr=0x0107, RotaForce_addr=0x0108, \
	AngleRelat_addr=0x0109, GripID_addr=0x0302, FBstart_addr=0x0200,WriteFlash_addr=0x0300, IOmodeEnable_addr=0x0402, \
	StopRota_addr=0x0502, ResetRotaVal_addr=0x0506, SeriesNum_addr=0x0A00
};



extern UART_PARAM_t uart_params[4];//串口对象
extern uint8_t Uart_ID_Group[5];//用来联系串口对象和PDO对象
extern uint32_t Time_Feedback;

uint8_t Scan_Connected_Gripper(void);
uint8_t Gripper_mode_Config(uint8_t timerID);
uint8_t ControlGripper(uint8_t used_port_number);
uint8_t Reset_Update_Member(void);
uint8_t Check_Gripper_Status(void);
uint8_t Check_Free_Port_Number(void);
bool Is_Control_Update(void);
void Check_PDO_Update(void);
uint8_t ReadGripperReg(GRIPPER_OBJECT *grip_ob, uint16_t regs_start_addr, uint8_t number);
uint8_t WriteGripperReg(GRIPPER_OBJECT *grip_ob, uint16_t reg_addr, uint16_t value);
uint8_t WriteGripperMultiReg(GRIPPER_OBJECT *grip_ob, uint16_t regs_start_addr, uint8_t number, uint8_t *tx_buf);
uint8_t Gripper_Opreat(uint8_t grip_ob_index, uint8_t cmd_index);


#endif

